package com.example.demo.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Created by wen.gao on 2018/5/7.
 */
@Configuration
@ConfigurationProperties("swagger2")
@PropertySource("classpath:swagger2.properties")
public class Swagger2Config {

    private static final Logger logger = LogManager.getLogger(Swagger2Config.class);

    boolean enableSwagger;

    @Bean
    public Docket createRestApi() {
        logger.info("swagger-ui enabled set to " + enableSwagger);
        return new Docket(DocumentationType.SWAGGER_2).
                enable(enableSwagger)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        logger.info("swagger-ui enabled set to " + enableSwagger);
        return new ApiInfoBuilder()
                .title("Springboot2.0 demo API docs")
                .description("just description")
                .termsOfServiceUrl("http://gaowen.win")
                .version("1.0")
                .build();
    }

    public boolean getIsEnableSwagger() {
        return enableSwagger;
    }

    public void setEnableSwagger(boolean enableSwagger) {
        this.enableSwagger = enableSwagger;
    }
}